import { Animation, Canvas, Label, Node, ParticleSystem, UITransform, Vec2, Vec3, director, find, v3 } from "cc";
import { PoolMgr } from "./PoolMgr";

export class EffMgr {
    public static _ins: EffMgr = null

    public static get ins() {
        if (null == this._ins) {
            this._ins = new EffMgr();
        }
        return this._ins
    }

    show(name: string, parent: Node = null, pos: Vec3 = v3(0, 0, 0)) {
        if (!parent) {
            const canvas = director.getScene().getComponentInChildren(Canvas)
            parent = find('UI/EFFLAYER', canvas.node)
        }
        const eff = PoolMgr.ins.getNode(name, parent, pos)
        if (!eff) {
            console.log(`${name}不存在`)
            return
        }
        if (eff.getComponent(Animation)) {
            const anim = eff.getComponent(Animation)
            anim.on(Animation.EventType.FINISHED, () => {
                eff.removeFromParent()
            }, this)
        } else if (eff.getComponent(ParticleSystem)) {
            eff.getComponent(ParticleSystem).play()
        }
    }

    showTip(name: string, text: string) {
        const canvas = director.getScene().getComponentInChildren(Canvas)
        const layer = find('UI/EFFLAYER', canvas.node)
        const old = layer.getChildByName('EffTip')
        old && old.removeFromParent()
        const eff = PoolMgr.ins.getNode(name, layer)
        if (!eff) {
            console.log(`${name}不存在`)
            return
        }
        const tip = eff.getChildByName('tip')
        const label = tip.getChildByName('label')
        label.getComponent(Label).string = `${text}`
        const anim = eff.getComponent(Animation)
        anim.on(Animation.EventType.FINISHED, () => {
            eff.removeFromParent()
        }, this)
    }
}


